package com.bawei.lib_common.widget.nine_pic_grid

import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bawei.lib_common.R
import com.bawei.lib_common.dp
import com.bawei.lib_image.GlideApp
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target

/**
 *作者：lxy
 *功能:
 *时间：2025/6/3
 */
class NinePicsAdapter(private val mPicList:List<Image>, private val imageClick:(Int)->Unit ) : RecyclerView.Adapter<NinePicsAdapter.NinePicViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NinePicViewHolder {

        val imageView = ImageView(parent.context).apply {
            scaleType = ImageView.ScaleType.CENTER_CROP
            setBackgroundResource(R.drawable.shape_bg_mask_round_4dp)
            foreground = context.getDrawable(R.drawable.shape_bg_normal_ripple_with_round_mask)
        }
        when (viewType) {
            NINE.value -> {
                return NinePicViewHolder(imageView, 110.dp, 110.dp)
            }

            LANDSCAPE.value -> {
                return NinePicViewHolder(imageView, 230.dp, 170.dp)

            }

            PORTRAIT.value -> {
                return NinePicViewHolder(imageView, 230.dp, 310.dp)
            }
        }
        return NinePicViewHolder(imageView, 110.dp, 110.dp)
    }

    override fun getItemCount(): Int {
        return mPicList.size
    }

    override fun onBindViewHolder(holder: NinePicViewHolder, position: Int) {
        val imageItem = mPicList[position]
        val isVideo = imageItem.contentType?.contains("video") //判断是否为视频
        val thumbUrl = if(isVideo!!){ //图片
            imageItem.firstImage
        }else{
            imageItem.url
        }
        //展示图片
        GlideApp.with(holder.itemView.context)
            .load(thumbUrl)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(
                    e: GlideException?,
                    model: Any?,
                    target: Target<Drawable>,
                    isFirstResource: Boolean
                ): Boolean {
                    return  false
                }

                override fun onResourceReady(
                    resource: Drawable,
                    model: Any,
                    target: Target<Drawable>?,
                    dataSource: DataSource,
                    isFirstResource: Boolean
                ): Boolean {
                    //图片准备完成后可点击
                    holder.itemView.setOnClickListener {
                        imageClick.invoke(holder.absoluteAdapterPosition)
                    }
                    return  false
                }

            })
            .transform(RoundedCorners(4))
            .into(holder.itemView as ImageView)
    }


    inner class NinePicViewHolder(itemView: ImageView, width: Int, height: Int) :
        RecyclerView.ViewHolder(itemView) {
        init {
            itemView.layoutParams = ViewGroup.LayoutParams(width, height)
        }
    }


    sealed class ViewType(val value: Int)
    object NONE : ViewType(1)
    object NINE : ViewType(2)
    object PORTRAIT : ViewType(3) //竖屏
    object LANDSCAPE : ViewType(4) //横屏
    object VIDEO : ViewType(5)


}